<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>03-Vuex的基本使用</title>
  </head>
  <body>
    <div id="app">
      <h1>vue的例子</h1>
      <p>{{msg}}</p>
      <p>这是标题：{{$store.state.title}}</p>
      <p>这是数字：{{$store.state.count}}</p>
      <button @click="addCount">按钮</button>
    </div>
    <script src="./js/vue.js"></script>
    <script src="./js/vuex.js"></script>
    <script>
      Vue.config.productionTip = false // 关闭生产提示
      Vue.use(Vuex)
      const store = new Vuex.Store({
        strict: true,
        state: {
          title: 'vuex的使用',
          count: 0,
        },
        actions: {
          INCREMENT({ commit }) {
            setTimeout(function () {
              commit('increment', 2)
            }, 2000)
          },
        },
        mutations: {
          increment(state, n) {
            state.count += n
          },
        },
      })
      const vm = new Vue({
        el: '#app',
        data: {
          msg: 'hello',
        },
        store,
        methods: {
          addCount() {
            this.$store.dispatch('INCREMENT')
          },
        },
      })
      console.log(vm)
    </script>
  </body>
</html>
